/*
 WebServiceController.m
 LegislatorLookup
 
 This controller contains all the web services used to retrieve data
 
 Copyright (c) 2009 Nabil Ahmad
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 
 1. Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the 
 documentation and/or other materials provided with the distribution. 
 
 3. The name of the author may not be used to endorse or promote products
 derived from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */	

#import "WebServiceController.h"
#import "JSON.h"
#import "Legislator.h"

#define SUNLIGHT_API_KEY @"API_KEY_HERE"
#define OPEN_SECRETS_API_KEY @"API_KEY_HERE"

@implementation WebServiceController

// Retrieves all the legislators for a specified state
+ (NSArray *) getLegislatorsForState:(NSString *)state {
	NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://services.sunlightlabs.com/api/legislators.getList.json?apikey=%@&state=%@", SUNLIGHT_API_KEY, state]];
	NSString *json = [NSString stringWithContentsOfURL:url];
    NSArray *jsonObj = [[[json JSONValue] objectForKey:@"response"] objectForKey:@"legislators"];
	
	NSMutableArray *sen = [[NSMutableArray alloc] init];
	NSMutableArray *rep = [[NSMutableArray alloc] init];
	NSArray *legislators = [[NSArray alloc] initWithObjects:sen, rep, nil];

	// Loop through objects to package in Legislator obj
	for (NSDictionary *dict in jsonObj) {		
		Legislator *legislator = [[Legislator alloc] initWithData:[dict objectForKey:@"legislator"]];

		// Add items in seperate arrays so that display can have multiple sections
		if ([[legislator.legislatorData objectForKey:@"title"] isEqualToString:@"Rep"])
			[rep addObject:legislator];
		else
			[sen addObject:legislator];
		
	}
	
	return legislators;
}

// Retrieves a list of legislators via search
+ (NSMutableDictionary *) getLegislatorsSearch:(NSString *)searchTerm {
	NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://services.sunlightlabs.com/api/legislators.search.json?apikey=%@&name=%@", SUNLIGHT_API_KEY, searchTerm]];
	NSString *json = [NSString stringWithContentsOfURL:url];
    NSArray *jsonObj = [[[json JSONValue] objectForKey:@"response"] objectForKey:@"results"];
	
	NSMutableDictionary *legislators = [[NSMutableDictionary alloc] init];

	// Loop through objects to package in Legislator obj
	for (NSDictionary *dict in jsonObj) {		
		Legislator *legislator = [[Legislator alloc] initWithData:[[dict objectForKey:@"result"] objectForKey:@"legislator"]];
		
		// only retrieve legislators in office
		if ([[legislator.legislatorData objectForKey:@"in_office"] intValue] == 1) {
			[legislators setObject:legislator forKey:[legislator displayName]];
		}
	}
	
	return legislators;
}

// Retrieves a user's Twitter Feed
+ (NSArray *)getTimelineForUsername:(NSString *)username
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://twitter.com/statuses/user_timeline/%@.json", username]];
    NSString *string = [NSString stringWithContentsOfURL:url];
	
    return [string JSONValue];    
}

// Retrieve top contributions
+ (NSDictionary *) getCandidateTopContributions:(NSString *)candidateId {
	NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"http://www.opensecrets.org/api/?method=candContrib&apikey=%@&cid=%@&output=json", OPEN_SECRETS_API_KEY,candidateId]];
	NSString *json = [NSString stringWithContentsOfURL:url];
	NSDictionary *jsonObj = nil;
	
	if (json.length > 0)
		jsonObj = [[[json JSONValue] objectForKey:@"response"] objectForKey:@"contributors"];

	return jsonObj;
}

// Retrieve Industry Contributions
+ (NSDictionary *) getCandidateIndustryContributions:(NSString *)candidateId {
	NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"http://www.opensecrets.org/api/?method=candIndustry&apikey=%@&cid=%@&output=json", OPEN_SECRETS_API_KEY,candidateId]];
	NSString *json = [NSString stringWithContentsOfURL:url];
	NSDictionary *jsonObj = nil;
	
	if (json.length > 0)
		jsonObj = [[[json JSONValue] objectForKey:@"response"] objectForKey:@"industries"];
	
	return jsonObj;
}

// Retrieve Candidate Summary
+ (NSDictionary *) getCandidateContributionSummary:(NSString *)candidateId {
	NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"http://www.opensecrets.org/api/?method=candSummary&apikey=%@&cid=%@&output=json", OPEN_SECRETS_API_KEY,candidateId]];
	NSString *json = [NSString stringWithContentsOfURL:url];
	NSDictionary *jsonObj = nil;
	
	if (json.length > 0)
		jsonObj = [[[[json JSONValue] objectForKey:@"response"] objectForKey:@"summary"] objectForKey:@"@attributes"];
	
	return jsonObj;
}

// Retrieve Candidate's PFD
+ (NSDictionary *) getPFDStatement:(NSString *)candidateId {
	NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"http://www.opensecrets.org/api/?method=memPFDprofile&apikey=%@&cid=%@&output=json", OPEN_SECRETS_API_KEY,candidateId]];
	NSString *json = [NSString stringWithContentsOfURL:url];
	NSDictionary *jsonObj = nil;

	if (json.length > 0)
		jsonObj = [[[json JSONValue] objectForKey:@"response"] objectForKey:@"member_profile"];
	
	return jsonObj;
}

@end
